//
//  UIAlertController+QTAdd.m
//  Pods
//
//  Created by 张俊博 on 16/9/7.
//
//

#import "UIAlertController+QTAdd.h"
#import "UIViewController+QTAdd.h"
#import "QTCategoryMacro.h"
#import "UIActionSheet+QTAdd.h"

static NSInteger const UIAlertControllerBlocksCancelButtonIndex = 0;
static NSInteger const UIAlertControllerBlocksDestructiveButtonIndex = 1;
static NSInteger const UIAlertControllerBlocksFirstOtherButtonIndex = 2;

@implementation UIAlertController (QTAdd)

+ (instancetype)showInViewController:(UIViewController *)viewController
                           withTitle:(NSString *)title
                             message:(NSString *)message
                      preferredStyle:(UIAlertControllerStyle)preferredStyle
                   cancelButtonTitle:(NSString *)cancelButtonTitle
              destructiveButtonTitle:(NSString *)destructiveButtonTitle
                   otherButtonTitles:(NSArray *)otherButtonTitles
#if TARGET_OS_IOS
  popoverPresentationControllerBlock:(void(^)(UIPopoverPresentationController *popover))popoverPresentationControllerBlock
                          sourceRect:(CGRect)rect
#endif
                            tapBlock:(UIAlertControllerCompletionBlock)tapBlock
{
    
    if (isNullStr(title) && isNullStr(message) && preferredStyle==UIAlertControllerStyleAlert) {
        return nil;
    }
    
    UIAlertController *strongController = [self alertControllerWithTitle:title
                                                                 message:message
                                                          preferredStyle:preferredStyle];
    
    __weak UIAlertController *controller = strongController;
    
    if (cancelButtonTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction *action){
                                                                 if (tapBlock) {
                                                                     tapBlock(controller, action, UIAlertControllerBlocksCancelButtonIndex);
                                                                 }
                                                             }];
        //        // 设置按钮背景图片
        //        UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //        [cancelAction setValue:accessoryImage forKey:@"image"];
        //        // 设置按钮的title颜色
        //        [cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
        //        // 设置按钮的title的对齐方式
        //        [cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
        [controller addAction:cancelAction];
    }
    
    if (destructiveButtonTitle) {
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle
                                                                    style:UIAlertActionStyleDestructive
                                                                  handler:^(UIAlertAction *action){
                                                                      if (tapBlock) {
                                                                          tapBlock(controller, action, UIAlertControllerBlocksDestructiveButtonIndex);
                                                                      }
                                                                  }];
        //        // 设置按钮背景图片
        //        UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //        [destructiveAction setValue:accessoryImage forKey:@"image"];
        //        // 设置按钮的title颜色
        //        [destructiveAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
        //        // 设置按钮的title的对齐方式
        //        [destructiveAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
        [controller addAction:destructiveAction];
    }
    
    for (NSUInteger i = 0; i < otherButtonTitles.count; i++) {
        NSString *otherButtonTitle = otherButtonTitles[i];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle
                                                              style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction *action){
                                                                if (tapBlock) {
                                                                    tapBlock(controller, action, UIAlertControllerBlocksFirstOtherButtonIndex + i);
                                                                }
                                                            }];
        [controller addAction:otherAction];
    }
    
#if TARGET_OS_IOS
    if (popoverPresentationControllerBlock) {
        popoverPresentationControllerBlock(controller.popoverPresentationController);
    }
#endif
    
    //    //修改title
    //    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    //    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    //    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    //    if ([controller valueForKey:@"attributedTitle"]) {
    //        [controller setValue:alertControllerStr forKey:@"attributedTitle"];
    //    }
    //    //修改message
    //    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
    //    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
    //    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
    //    if ([controller valueForKey:@"attributedMessage"]) {
    //        [controller setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    //    }
    if (iPad) {
        controller.modalPresentationStyle = UIModalPresentationPopover;
        controller.popoverPresentationController.sourceView = [UIViewController  currentViewController].view;
        controller.popoverPresentationController.sourceRect = rect;
        [viewController presentViewController:controller animated:YES completion:nil];
        return controller;
    } else {
        [viewController presentViewController:controller animated:YES completion:nil];
        return controller;
    }
    
    
}

+ (instancetype)showAlertInViewController:(UIViewController *)viewController
                                withTitle:(NSString *)title
                                  message:(NSString *)message
                        cancelButtonTitle:(NSString *)cancelButtonTitle
                   destructiveButtonTitle:(NSString *)destructiveButtonTitle
                        otherButtonTitles:(NSArray *)otherButtonTitles
                                 tapBlock:(UIAlertControllerCompletionBlock)tapBlock
{
    return [self showInViewController:viewController
                            withTitle:title
                              message:message
                       preferredStyle:UIAlertControllerStyleAlert
                    cancelButtonTitle:cancelButtonTitle
               destructiveButtonTitle:destructiveButtonTitle
                    otherButtonTitles:otherButtonTitles
#if TARGET_OS_IOS
   popoverPresentationControllerBlock:nil
                           sourceRect:CGRectZero
#endif
                             tapBlock:tapBlock];
}

+ (instancetype)showAlertWithTitle:(NSString *)title
                           message:(NSString *)message
                 cancelButtonTitle:(NSString *)cancelButtonTitle
                 otherButtonTitles:(NSArray *)otherButtonTitles
                          tapBlock:(UIAlertControllerCompletionBlock)tapBlock
{
    
    UIViewController *vc = [UIViewController currentNavigationViewController];
    if (!vc) {
        vc = [UIViewController currentViewController];
    }
    return [self showAlertInViewController:vc //[UIViewController currentNavigationViewController]
                                 withTitle:title
                                   message:message
                         cancelButtonTitle:cancelButtonTitle
                    destructiveButtonTitle:nil
                         otherButtonTitles:otherButtonTitles
                                  tapBlock:tapBlock];
}

+ (nonnull instancetype)showActionSheetInViewController:(nonnull UIViewController *)viewController
                                              withTitle:(nullable NSString *)title
                                                message:(nullable NSString *)message
                                      cancelButtonTitle:(nullable NSString *)cancelButtonTitle
                                 destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
                                      otherButtonTitles:(nullable NSArray *)otherButtonTitles
#if TARGET_OS_IOS
                     popoverPresentationControllerBlock:(nullable UIAlertControllerPopoverPresentationControllerBlock)popoverPresentationControllerBlock
                                             sourceRect:(CGRect)rect
#endif
                                               tapBlock:(nullable UIAlertControllerCompletionBlock)tapBlock
{
    return [self showInViewController:viewController
                            withTitle:title
                              message:message
                       preferredStyle:UIAlertControllerStyleActionSheet
                    cancelButtonTitle:cancelButtonTitle
               destructiveButtonTitle:destructiveButtonTitle
                    otherButtonTitles:otherButtonTitles
#if TARGET_OS_IOS
   popoverPresentationControllerBlock:popoverPresentationControllerBlock
                           sourceRect:rect
#endif
                             tapBlock:tapBlock];
}

+ (nonnull instancetype)showActionSheetWithTitle:(nullable NSString *)title
                                         message:(nullable NSString *)message
                               cancelButtonTitle:(nullable NSString *)cancelButtonTitle
                               otherButtonTitles:(nullable NSArray *)otherButtonTitles
#if TARGET_OS_IOS
                                      sourceRect:(CGRect)rect
#endif
                                        tapBlock:(nullable UIAlertControllerCompletionBlock)tapBlock
{
    UIViewController *vc = [UIViewController currentNavigationViewController];
    if (!vc) {
        vc = [UIViewController currentViewController];
    }
    return [self showActionSheetInViewController:vc //[UIViewController currentNavigationViewController]
                                       withTitle:title
                                         message:message
                               cancelButtonTitle:cancelButtonTitle
                          destructiveButtonTitle:nil
                               otherButtonTitles:otherButtonTitles
              popoverPresentationControllerBlock:nil
#if TARGET_OS_IOS
                                      sourceRect:rect
#endif
                                        tapBlock:tapBlock];
}

#pragma mark -

- (BOOL)visible
{
    return self.view.superview != nil;
}

- (NSInteger)cancelButtonIndex
{
    return UIAlertControllerBlocksCancelButtonIndex;
}

- (NSInteger)firstOtherButtonIndex
{
    return UIAlertControllerBlocksFirstOtherButtonIndex;
}

- (NSInteger)destructiveButtonIndex
{
    return UIAlertControllerBlocksDestructiveButtonIndex;
}

@end
